//
//  XWHADeviceCenterConnectView.swift
//  XWHAOfflineFitting
//
//  Created by edy on 2025/4/27.
//

import UIKit
import DMRCommonBaseLib


extension XWHADeviceCenterConnectView {
    
    class func getDevicePortLb(devicePort: XWDevicePortType, text: String) -> UILabel {
        let retLb = UILabel()
        let font = AppFont.harmonyOSSans(ofSize: 19, weight: .bold)
        // 左右颜色
        let leftEarColor = UIColor(hex: 0x007AFF)!
        let rightEarColor = UIColor(hex: 0xEE704B)!
        
        if devicePort == .left {
            retLb.textColor = leftEarColor
        } else {
            retLb.textColor = rightEarColor
        }
        retLb.font = font
        retLb.textAlignment = .center
        retLb.layer.cornerRadius = 16
        
        retLb.text = text
        
        return retLb
    }
    
}

class XWHADeviceCenterConnectView: XWHBaseView {

    lazy var titleLb = UILabel()
    lazy var iconView = UIImageView()
    
    lazy var leftTitleLb = XWHADeviceCenterConnectView.getDevicePortLb(devicePort: .left, text: "L")
    lazy var rightTitleLb = XWHADeviceCenterConnectView.getDevicePortLb(devicePort: .right, text: "R")

    lazy var leftDeviceBtn = JXLayoutButton()
    lazy var rightDeviceBtn = JXLayoutButton()
    
    
    override func addSubViews() {
        super.addSubViews()
        
        addSubview(titleLb)
        addSubview(iconView)
        
        addSubview(leftTitleLb)
        addSubview(rightTitleLb)
        
        addSubview(leftDeviceBtn)
        addSubview(rightDeviceBtn)

        let font = AppFont.harmonyOSSans(ofSize: 16, weight: .medium)
        titleLb.font = font
        titleLb.textColor = majorTextColor
        titleLb.textAlignment = .left
        titleLb.adjustsFontSizeToFitWidth = true
        
        config(deviceBtn: leftDeviceBtn)
        config(deviceBtn: rightDeviceBtn)
        
        let leftImage = UIImage(named: "left_circle_icon")
        let rightImage = UIImage(named: "right_circle_icon")
        leftDeviceBtn.setImage(leftImage, for: .normal)
        leftDeviceBtn.layoutStyle = .leftTitleRightImage
        rightDeviceBtn.setImage(rightImage, for: .normal)
        rightDeviceBtn.layoutStyle = .leftImageRightTitle
    
        titleLb.text = ""
    }
    
    private func config(deviceBtn btn: JXLayoutButton) {
        let font = AppFont.harmonyOSSans(ofSize: 14, weight: .regular)
        btn.titleLabel?.font = font
        btn.setTitleColor(majorTextColor, for: .normal)
        btn.midSpacing = 4
    }
    
    func configBgBtn(btn: UIButton) {
        let font = AppFont.harmonyOSSans(ofSize: 14, weight: .regular)
        btn.titleLabel?.font = font
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.layer.cornerRadius = 15
        btn.layer.backgroundColor = btnBgColor.cgColor
        btn.layer.borderWidth = 0
        btn.layer.borderColor = nil
    }
    
    func configBorderBtn(btn: UIButton) {
        let font = AppFont.harmonyOSSans(ofSize: 14, weight: .regular)
        btn.titleLabel?.font = font
        btn.setTitleColor(btnBgColor, for: .normal)
        btn.layer.cornerRadius = 15
        btn.layer.borderWidth = 1
        btn.layer.borderColor = btnBgColor.cgColor
        btn.layer.backgroundColor = nil
    }
    
    func configDisableBtn(btn: UIButton) {
        let font = AppFont.harmonyOSSans(ofSize: 14, weight: .regular)
        btn.isEnabled = false
        btn.titleLabel?.font = font
        btn.setTitleColor(minorTextColor, for: .normal)
        btn.layer.cornerRadius = 0
        btn.layer.borderWidth = 0
        btn.layer.borderColor = nil
        btn.layer.backgroundColor = nil
    }
    
    override func relayoutSubViews() {
        leftTitleLb.snp.remakeConstraints { make in
            make.left.equalToSuperview()
            make.size.equalTo(32)
            make.centerY.equalToSuperview()
        }
    }

}
